package com.kuzan.autocloud.secutiry.helper;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.kuzan.autocloud.common.model.organization.Person;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>Description: </p>
 *
 * @author kuzank 2019/12/1
 */
@Service
public class JwtHelper {

    @Value("${jwt.secret.key}")
    private String secretKey;
    @Value("${token.expire.time}")
    private long tokenExpireTime;


    /**
     * 生成jwt
     *
     * @param person
     * @return
     */
    public String buildJWT(Person person) {

        Date now = new Date();
        Algorithm algo = Algorithm.HMAC256(secretKey);
        String token = JWT.create()
                .withIssuer("auto-cloud")
                .withIssuedAt(now)
                .withExpiresAt(new Date(now.getTime() + tokenExpireTime))
                .withClaim("person", person.getId()) // 保存身份标识
                .withClaim("organization", person.getOrganization())
                .sign(algo);
        return token;
    }

}
